<?php
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
/**
 * Class MoveRule
 * @package App\Model\Backstage
 * 公司房客自动转移规则model
 */
class MoveRule extends Base
{
    //房客自动转移规则数据表
    protected  $move_rule_table;
    public  $roomInfoModel;
    public  $customersModel;
    public $roomFollowModel;
    public $customersFollowModel;
    public $workSummaryModel;
    public function __construct()
    {
        parent::__construct();
        $this->roomInfoModel = new RoomInfo();
        $this->customersModel = new Customers();
        $this->move_rule_table = 'move_rule';
    }

    //转移类型
    public static $moveTypeArr = ['私盘转公盘', '私客转公客', '取消优质房'];
    //日期类型
    public static $dateTypeArr = ['最后跟进日期', '委托日期', '录入日期'];
    //转出类目
    public static $moveListArr = ['房源记录+跟进',  '客源记录+跟进', '工作总结'];
    //转换类目
    public static $turnArr = ['私盘转公盘', '公盘转私盘', '私客转公客', '公客转私客'];
    /**
     * @param $params
     * @return mixed
     * 添加规则
     */
    public  function addRule($params){
        $params['a_created_at'] = time();
        $params['a_b_id'] = $this->branch_id;
        $params['a_u_id'] = $this->uid;
        $id  =  DB::table($this->move_rule_table)->where('a_b_id', $this->branch_id)->where('a_move_type', $params['a_move_type'])->where('a_d_id', $params['a_d_id'])->value('a_id');
        if ($id) {
            return -1;
        }
        $res =  DB::table($this->move_rule_table)->insertGetId($params);
        if($res){
            $content = '添加规则ID => '.$res;
            $log = new Log();
            $log->addlog($this->store_id,$this->uid,13,$content);
        }
        return $res;
    }
    /**
     * @param $params
     * @return mixed
     * 修改规则
     */
    public function updateRule($params){
        $params['a_updated_at'] = time();
        $id  =  DB::table($this->move_rule_table)
            ->where('a_id', '<>', $params['a_id'])
            ->where('a_b_id', $this->branch_id)
            ->where('a_d_id', $params['a_d_id'])
            ->where('a_move_type', $params['a_move_type'])
            ->value('a_id');

        if ($id) {
            return -1;
        }
        $res =  DB::table($this->move_rule_table)->where('a_id', $params['a_id'])->update($params);
        if($res){
            $log = new Log();
            $content = '修改规则ID => '.$params['a_id'];
            $log->addlog($this->store_id,$this->uid,13,$content);
        }
        return $res;
    }

    //获取所有开启规则信息
    public function ExecutionRule(){
        $field = ['a_id','a_b_id','a_d_id','a_u_id','a_date_type','a_day_num','a_trade_type','a_m_status','a_use_type',
                'a_entrust_way','a_move_type','a_status'];
       $list =  DB::table($this->move_rule_table)
            ->where('a_b_id', $this->branch_id)
            ->where('a_status',1)
            ->select($field)
            ->orderBy('a_id', 'desc')
            ->get()
            ->toArray();
       if(sizeof($list) > 0){
           return array_map('get_object_vars', $list);
       }
        return false;
    }


    /**
     * @param $a_id
     * @return mixed
     * 删除规则
     */
    public function delRule($a_id){
        $res =  DB::table($this->move_rule_table)->where('a_id', $a_id)->delete();
        if($res){
            $log = new Log();
            $content = '删除规则ID => '.$a_id;
            $log->addlog($this->store_id,$this->uid,13,$content);
        }
        return $res;
    }

    /**
     * @return mixed
     * 获取规则列表
     */
    public function lists(){
        $field = [
            'a_id', 'a_d_id', 'a_date_type', 'a_day_num', 'a_trade_type', 'a_m_status', 'a_use_type', 'a_entrust_way', 'a_move_type', 'a_status',
            DB::raw('from_unixtime(a_created_at) as a_created_at'), DB::raw('from_unixtime(a_updated_at) as a_updated_at'),
            'name'
        ];
        return DB::table($this->move_rule_table)
            ->leftjoin('department', 'a_d_id', '=', 'id')
            ->where('a_b_id', $this->branch_id)
            ->select($field)
            ->orderBy('a_id', 'desc')
            ->get();
    }

    /**
     * @return mixed
     * 获取更新列表
     */
    public function updateList(){
        $map[] = ['a_b_id', $this->branch_id];
        $map[] = ['a_status', 1];
//        return MoveRule::where($map)->get();
        return DB::table($this->move_rule_table)->where($map)->get();
    }

    /**
     * @param $id
     * @return mixed
     * 获取单条规则详细信息
     */
    public  function getInfo($id){
//        return MoveRule::where('a_id', $id)->first()->toArray();
        return DB::table($this->move_rule_table)->where('a_id', $id)->first();
    }

    /**
     * @param $params
     * @return bool|mixed
     * 转移员工房客信息
     * 此处转移数量较大客户端需要提示用户在晚上或者下班时间进行操作
     */
    public function moves($params){
        $this->roomFollowModel = new RoomFollow();
        $this->customersFollowModel = new CustomersFollow();
        $this->workSummaryModel = new WorkSummary();
        $move_type = explode(',',$params['move_type']);
        DB::beginTransaction();
        //0房源记录+跟进 1客源记录+跟进 2工作总结
        try {
            if (in_array(0, $move_type)) {
                //房源跟进
                $this->roomFollowModel->moves($this->roomInfoModel, $params);
                //房源记录
                $this->roomInfoModel->moves($params, 1);
            }
            if (in_array(1, $move_type)) {
                //客源跟进
                $this->customersFollowModel->moves($this->customersModel, $params);
                //客源记录
                $this->customersModel->moves($params, 1);
            }
            if (in_array(2, $move_type)) {
                //工作总结
                $this->workSummaryModel->moves($params);
            }
            DB::commit();
            return true;
        }
        catch(Exception $e)
        {
            DB::rollback();
            return false;
        }
    }

    /**
     * @param $params
     * 公私盘 公私客相互转换
     */
    public function turns($params){
        if($params['turn_type'] == 0 || $params['turn_type'] == 1){ //私盘转公盘 公盘转私盘
            return $this->roomInfoModel->turns($params);
        }elseif($params['turn_type'] == 2 || $params['turn_type'] == 3){ //私客转公客 公客转私客
            return $this->customersModel->turns($params);
        }
        return -1;
    }
}